﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

using SuperMapLib;

namespace JR_RouteAlarm
{
    class BufferLayer : IWorkspace
    {
        /*変数*/
        public static string strMapName = String.Empty;

        private AxSuperMapLib.AxSuperWorkspace superWksp = null;
        private AxSuperMapLib.AxSuperMap objMap = null;
        private soDatasets objDSs = null;
        private string strLayerName = String.Empty;

        public BufferLayer(AxSuperMapLib.AxSuperWorkspace superWksp, AxSuperMapLib.AxSuperMap objMap)
        {
            this.superWksp = superWksp;
            this.objMap = objMap;
        }

        /// <summary>
        /// バッファーレイヤーをマップに置いて行く
        /// </summary>
        /// <param name="objLayers"></param>
        private void AddBufferLayerToMap(soLayers objLayers)
        {
            soLayer objLayerSrc = null;

            objLayerSrc = objLayers["joukoujouR@areamap"];
            if (!objLayers.MoveTo(objLayers.Count, objLayers.FindLayer(objLayerSrc)))
            {
                throw new Exception("Layer move failed!");
            }

            Marshal.ReleaseComObject(objLayerSrc);
            objLayerSrc = null;
        }

        /// <summary>
        /// レイヤーに主題図を追加
        /// </summary>
        /// <param name="objLayer"></param>
        private void SetTheme(soLayer objLayer)
        {
            SetColor(objLayer);
            SetLabel(objLayer);
        }

        /// <summary>
        /// 色分けの主題図を作成
        /// </summary>
        /// <param name="objLayer"></param>
        private void SetColor(soLayer objLayer)
        {
            soThemeUnique objThemeUnique = null;

            objThemeUnique = objLayer.ThemeUnique;

            objThemeUnique.Field = "areaID"; //Utility.CROSS_AREA_FIELD;
            objThemeUnique.Caption = Utility.THEME_COLOR_CAPTION;
            objThemeUnique.MakeDefault();

            for (int i = 0; i < objThemeUnique.ValueCount; i++)
            {
                objThemeUnique.set_ItemCaption(i, i.ToString());
            }

            //Marshal.ReleaseComObject(objStyle);
            //objStyle = null;
            //Marshal.ReleaseComObject(objColors);
            //objColors = null;
            Marshal.ReleaseComObject(objThemeUnique);
            objThemeUnique = null;
        }

        /// <summary>
        /// ラベル分けの主題図を作成
        /// </summary>
        /// <param name="objLayer"></param>
        private void SetLabel(soLayer objLayer)
        {
            soThemeLabel objThemeLabel = null;
            soTextStyle objTextStyle = null;
            StringBuilder sbSqlLabel = null;

            objThemeLabel = objLayer.ThemeLabel;
            sbSqlLabel = new StringBuilder();

            sbSqlLabel.AppendFormat("{0} + {1} + '～' + {2} + {3}",
                RouteDataset.FLD_AREA_CD, RouteDataset.FLD_KIROTEI_BGN, RouteDataset.FLD_KIROTEI_END, RouteDataset.FLD_EXTRA_INFO);
            for (int i = 1; i <= Utility.MAX_ALARM_TARGETS; i++)
            {
                sbSqlLabel.AppendFormat(" + {1}{0} + {2}{0}",
                    i, RouteDataset.FLD_ALARM_ROUTE, RouteDataset.FLD_ALARM_AREA);
            }

            objThemeLabel.Field = sbSqlLabel.ToString();

            objThemeLabel.Caption = Utility.THEME_LABLE_CAPTION;
            objThemeLabel.BackShape = seTextBackShape.sctBackRoundRect;
            objThemeLabel.AutoAvoidOverlapped = true;
            objThemeLabel.IgnoreSmallObject = false;
            //objThemeLabel.LineDirection = seLabelLineDirection.sclDirectionLeftBottomToRightTop;
            objThemeLabel.ShowLeaderLines = true;
            objThemeLabel.EnableFlow = false;
            objThemeLabel.TextControlMode = seTextControlMode.scmTextControlWrapText;
            objThemeLabel.SingleLineCharCount = Utility.MAX_LINE_CHARS;
            objThemeLabel.OnTop = true;
            objThemeLabel.OffsetX = 150D;
            objThemeLabel.OffsetY = 150D;

            objTextStyle = new soTextStyle();
            objTextStyle.FontHeight = Utility.LABEL_FONT_HIGHT;
            objTextStyle.FontWidth = Utility.LABEL_FONT_WIDTH;
            objTextStyle.Color = System.Drawing.Color.Red.R;
            //objTextStyle.Align = seTextAlign.sctBaslineLeft;
            objThemeLabel.TextStyle = objTextStyle;

            Marshal.ReleaseComObject(objTextStyle);
            objTextStyle = null;
            Marshal.ReleaseComObject(objThemeLabel);
            objThemeLabel = null;
        }

        #region IWorkspace メンバ

        public void Initialize()
        {
            soMaps objMaps = null;

            objMaps = superWksp.Maps;
            strMapName = objMaps[1];
            objMap.Connect(superWksp.CtlHandle);
            if (!objMap.OpenMap(strMapName))
            {
                throw new Exception("Open map failed!");
            }

            this.objDSs = RouteDataset.objSource.Datasets;

            Marshal.ReleaseComObject(objMaps);
            objMaps = null;
        }

        public void SaveToWksp()
        {
            soLayers objLayers = null;
            soLayer objLayer = null;

            objLayers = objMap.Layers;
            strLayerName = objDSs[objDSs.Count].Name + "@" + Utility.s_DatasourceAlias;
            objLayer = objLayers[strLayerName];
            if (objLayer != null)
            {
                objLayers.RemoveAt(objLayers.FindLayer(objLayer));
            }

            objLayer = objLayers.AddDataset(objDSs[objDSs.Count], false);
            AddBufferLayerToMap(objLayers);
            SetTheme(objLayer);

            objMap.SaveMap();

            Marshal.ReleaseComObject(objLayer);
            objLayer = null;
            Marshal.ReleaseComObject(objLayers);
            objLayers = null;
        }

        public void OutputAsFile()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void ReleaseMemory()
        {
            objMap.Close();
            objMap.Disconnect();

            //Marshal.ReleaseComObject(objMap); //COMではない？
            objMap = null;
            //Marshal.ReleaseComObject(objDSs);
            //objDSs = null;
            Marshal.ReleaseComObject(objDSs);
            objDSs = null;
        }

        #endregion
    }
}
